package com.study4j.common.log;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.study4j.common.model.LoggerMessage;

/**   
 * @description  日志队列
 * @author jiangbin   
 * @date 2018年10月31日 上午11:47:31 
 * @version 1.0.0  
 */
public class LoggerQueue {
	// 队列大小
	public static final int QUEUE_MAX_SIZE = 10000;
	
	private static LoggerQueue alarmMessageQueue = new LoggerQueue();
	// 阻塞队列
	private BlockingQueue blockingQueue = new LinkedBlockingQueue<>(QUEUE_MAX_SIZE);

	private LoggerQueue() {
	}

	public static LoggerQueue getInstance() {
		return alarmMessageQueue;
	}

	/**
	 * 消息入队
	 *
	 * @param log
	 * @return
	 */
	public boolean push(LoggerMessage log) {
		return this.blockingQueue.add(log);// 队列满了就抛出异常，不阻塞
	}

	/**
	 * 消息出队
	 *
	 * @return
	 */
	public LoggerMessage poll() {
		LoggerMessage result = null;
		try {
			result = (LoggerMessage) this.blockingQueue.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return result;
	}

}
